package com.yanqu.road.server.manger.activity.shushan;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.args.ConditionTypeValueArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.rank.ActivityRankMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 蜀山个人排名
 * @Author cwq
 * @Data 2022/3/18 14:23
 */
public class ShuShanRankActivityMgr extends TempMgr {

    private static Map<Integer, Map<Long, Map<Integer, List<UserActivityConditionData>>>> userActivityConditionDataMap = new ConcurrentHashMap<>();


    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        userActivityConditionDataMap = new ConcurrentHashMap<>();
        if(ShuShanMgr.getActivityInfo() != null){
            List<Integer> activityIds = new ArrayList<>();
            activityIds.add(ShuShanMgr.getActivityInfo().getActivityId());
            if (activityIds.size() > 0) {
                userActivityConditionDataMap = UserActivityBussiness.getActivityAllConditionData(activityIds);
            }
        }
        return true;
    }

    public static List<UserActivityConditionData> getUserActivityConditionData(ActivityInfo activityInfo, long userId) {
        synchronized (userActivityConditionDataMap) {
            int activityId = activityInfo.getActivityId();
            List<UserActivityConditionData> list = new ArrayList<>();
            Map<Long, Map<Integer, List<UserActivityConditionData>>> userConditionMap = userActivityConditionDataMap.get(activityId);
            if (userConditionMap == null) {
                userActivityConditionDataMap.put(activityId, new ConcurrentHashMap<>());
                userConditionMap = userActivityConditionDataMap.get(activityId);
            }
            Map<Integer, List<UserActivityConditionData>> listMap = userConditionMap.get(userId);
            if (listMap == null) {
                listMap = new ConcurrentHashMap<>();
                userConditionMap.put(userId, listMap);
            }
            List<ActivityConditionInfo> activityConditionInfoList = NormalActivityMgr.getActivityConditionInfoList(activityId);
            for (ActivityConditionInfo activityConditionInfo : activityConditionInfoList) {
                if (activityConditionInfo.getType() > 0 ) {
                    if (isContinueAddCondition(activityInfo, activityConditionInfo.getType(), listMap.keySet())) {
                        if (listMap.get(activityConditionInfo.getType()) == null || !hasConditionData(listMap.get(activityConditionInfo.getType()), activityConditionInfo.getConditionId())) {
                            UserInfo userInfo = UserMgr.getUserInfo(userId);
                            //新的条件
                            UserActivityConditionData userConditionData = NormalActivityMgr.initUserActivityConditionData(userId, userInfo != null ? userInfo.getNickName() : "",
                                    BigInteger.ZERO, activityId, activityConditionInfo.getConditionId(), activityConditionInfo.getType());
                            addUserActivityConditionData(userConditionData);
                        }
                    }
                }
            }
            for (List<UserActivityConditionData> dataList : listMap.values()) {
                list.addAll(dataList);
            }
            return list;
        }
    }

    private static boolean isContinueAddCondition(ActivityInfo activityInfo, int type, Set<Integer> conditionTypeList) {
        if (!ActivityMgr.isRankActivityCondition(activityInfo, type)) {
            return true;
        } else {
            for (Integer conditionType : conditionTypeList) {
                if (conditionType == type) {
                    return false;
                }
            }
            return true;
        }
    }


    //添加新的活动条件
    public static UserActivityConditionData addUserActivityConditionData(UserActivityConditionData userConditionData) {
        synchronized (userActivityConditionDataMap) {
            long userId = userConditionData.getUserId();
            int activityId = userConditionData.getActivityId();
            if (!userActivityConditionDataMap.containsKey(userConditionData.getActivityId())) {
                userActivityConditionDataMap.put(userConditionData.getActivityId(), new ConcurrentHashMap<>());
            }
            if (!userActivityConditionDataMap.get(activityId).containsKey(userId)) {
                userActivityConditionDataMap.get(activityId).put(userId, new ConcurrentHashMap<>());
            }
            if (!userActivityConditionDataMap.get(activityId).get(userId).containsKey(userConditionData.getType())) {
                userActivityConditionDataMap.get(activityId).get(userId).put(userConditionData.getType(), new ArrayList<>());
            }
            if (!hasConditionData(userActivityConditionDataMap.get(activityId).get(userId).get(userConditionData.getType()), userConditionData.getConditionId())) {
                userActivityConditionDataMap.get(activityId).get(userId).get(userConditionData.getType()).add(userConditionData);
            }
            return getConditionData(userActivityConditionDataMap.get(activityId).get(userId).get(userConditionData.getType()), userConditionData);
        }
    }

    private static boolean hasConditionData(List<UserActivityConditionData> userActivityConditionDataList, int conditionInfoId) {
        return userActivityConditionDataList.stream().anyMatch(data -> data.getConditionId() == conditionInfoId);
    }

    private static UserActivityConditionData getConditionData(List<UserActivityConditionData> userActivityConditionDataList, UserActivityConditionData userConditionData) {
        for (UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
            if (userActivityConditionData.getConditionId() == userConditionData.getConditionId()) {
                return userActivityConditionData;
            }
        }
        return null;
    }

    /**
     * @param playerId
     * @param args
     */
    public static void notifyFinishEncounterEvent(long playerId, ConditionTypeValueArgs args) {
        synchronized (userActivityConditionDataMap) {
            if (args != null) {
                ActivityInfo activityInfo = ShuShanMgr.getActivityInfo();
                if (!ActivityMgr.activityInTime(activityInfo)) {
                    return;
                }
                Map<Long, Map<Integer, List<UserActivityConditionData>>> userConditionMap = userActivityConditionDataMap.get(activityInfo.getActivityId());
                if (userConditionMap != null) {
                    List<UserActivityConditionData> conditionDataList = getUserActivityConditionData(activityInfo, playerId);
                    if (conditionDataList != null) {
                        for (UserActivityConditionData conditionData : conditionDataList) {
                            if (conditionData.getType() == eGamePlayerEventType.ShuShanFinishEncounter.getValue()) {
                                if (conditionData.isGetReward()) {
                                    continue;
                                }
                                ActivityConditionInfo activityConditionInfo = NormalActivityMgr.getActivityConditionInfo(activityInfo.getActivityId(), conditionData.getConditionId());
                                if (activityConditionInfo == null) {
                                    continue;
                                }
                                if (args.getType() == activityConditionInfo.getParamList().get(0).intValue()) {
                                    conditionData.setValue(conditionData.getValue().add(BigInteger.valueOf(args.getValue())));
                                }
                                syncActivity(activityInfo.getActivityId(), conditionDataList, GamePlayerMgr.getOnlinePlayer(playerId)); // 同步客户端
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * @param playerId
     * @param totalValue
     */
    public static void notifyRankEvent(long playerId, long totalValue, String param) {
        synchronized (userActivityConditionDataMap) {
            if (totalValue > 0) {
                ActivityInfo activityInfo = ShuShanMgr.getActivityInfo();
                if (!ActivityMgr.activityInTime(activityInfo)) {
                    return;
                }
                Map<Long, Map<Integer, List<UserActivityConditionData>>> userConditionMap = userActivityConditionDataMap.get(activityInfo.getActivityId());
                if (userConditionMap != null) {
                    List<UserActivityConditionData> conditionDataList = getUserActivityConditionData(activityInfo, playerId);
                    if (conditionDataList != null) {
                        for (UserActivityConditionData conditionData : conditionDataList) {
                            if (conditionData.getType() == eGamePlayerEventType.ShuShanMaxMagicUserRank.getValue()) {
                                if (conditionData.isGetReward()) {
                                    continue;
                                }
                                conditionData.setValue(BigInteger.valueOf(totalValue));
                                conditionData.setParam(param);
                                syncActivity(activityInfo.getActivityId(), conditionDataList, GamePlayerMgr.getOnlinePlayer(playerId)); // 同步客户端
                                if (conditionData.getValue().compareTo(BigInteger.ZERO) > 0) {
                                    ActivityRankMgr.changeActivityRank(playerId, conditionData.getActivityId(), conditionData.getType(),
                                            conditionData.getValue(), conditionData.getParam());
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private static void syncActivity(int activityId, List<UserActivityConditionData> userActivityConditionDataList, GamePlayer player) {
        if (null == player || null == userActivityConditionDataList) {
            return;
        }
        player.sendPacket(Protocol.U_ACTIVITY_SYNC, ActivityPb.syncActivityConditionData(activityId, userActivityConditionDataList));
    }

    @Override
    public boolean save() {
        for (Map<Long, Map<Integer, List<UserActivityConditionData>>> userConditionMap : userActivityConditionDataMap.values()) {
            for (Map<Integer, List<UserActivityConditionData>> conditionMap : userConditionMap.values()) {
                for (List<UserActivityConditionData> list : conditionMap.values()) {
                    for (UserActivityConditionData conditionData : list) {
                        if (conditionData.isInsertOption()) {
                            UserActivityBussiness.addUserActivityConditionData(conditionData);
                        } else if (conditionData.isUpdateOption()) {
                            UserActivityBussiness.updateUserActivityConditionData(conditionData);
                        }
                    }
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
